<?php
namespace Application\Repository;
use Doctrine\ORM\EntityRepository;

class UserRepository extends AbstractRepository
{
     protected  $entity="Application\\Entity\\User";
    
    public function getByUid($uid,$number = 30)
    {
        $dql = "SELECT c FROM $this->entity c WHERE c.uid=?1";

        return $this->getEntityManager()->createQuery($dql)
                             ->setParameter(1, $uid)
                             ->setMaxResults($number)
                             ->getResult();
    }
    
    public function getUniqueByUid($uid,$number = 1)
    {
        $dql = "SELECT c FROM $this->entity c WHERE c.uid=?1";

        $results = $this->getEntityManager()->createQuery($dql)
                             ->setParameter(1, $uid)
                             ->setMaxResults($number)
                             ->getResult();
        return ($results!=null&&count($results)>0)?$results[0]:null;
    }
    
    
    public function getByUsername($username,$number = 30)
    {
    	$dql = "SELECT c FROM $this->entity c WHERE c.uid=?1";
    
    	return $this->getEntityManager()->createQuery($dql)
    	->setParameter(1, $username)
    	->setMaxResults($number)
    	->getResult();
    }

    public function getByEmailAndPassword($email,$password){
    	$dql = "SELECT c FROM $this->entity c WHERE c.email=?1 AND c.password = ?2";
    	$results = $this->getEntityManager()->createQuery($dql)
        	->setParameter(1, $email)
        	->setParameter(2, $password)
        	->setMaxResults(1)
        	->getResult();
    	return ($results!=null && count($results)>0)?$results[0]:null;
    }
    
    public function getByEmail($email,$number = 1)
    {
    	$dql = "SELECT c FROM $this->entity c WHERE c.email=?1";
    
    	return $this->getEntityManager()->createQuery($dql)
    	->setParameter(1, $email)
    	->setMaxResults($number)
    	->getResult();
    }
    public function getUniqueByEmail($email,$number = 1)
    {
    	$dql = "SELECT c FROM $this->entity c WHERE c.email=?1";
    
    	$results = $this->getEntityManager()->createQuery($dql)
    	->setParameter(1, $email)
    	->setMaxResults($number)
    	->getResult();
    	return ($results!=null && count($results)>0)?$results[0]:null;
    }
    
    public function getByKeyPassword($key,$number = 1)
    {
    	$dql = "SELECT c FROM $this->entity c WHERE c.keyPassword=?1";
    
    	return $this->getEntityManager()->createQuery($dql)
    	->setParameter(1, $key)
    	->setMaxResults($number)
    	->getResult();
    }
    

    public function getByKeyConfirm($key,$number = 1)
    {
    	$dql = "SELECT c FROM $this->entity c WHERE c.keyConfirm=?1";
    
    	return $this->getEntityManager()->createQuery($dql)
    	->setParameter(1, $key)
    	->setMaxResults($number)
    	->getResult();
    }
    
    public function getByEmailKeyConfirm($email,$key,$number = 1)
    {
    	$dql = "SELECT c FROM $this->entity c WHERE c.email=?1 AND c.keyConfirm=?2";
    
    	return $this->getEntityManager()->createQuery($dql)
    	->setParameter(1, $email)
    	->setParameter(2, $key)
    	->setMaxResults($number)
    	->getResult();
    }
    
    
    public function getByCountryId($countryId,$number = 30)
    {
    	$dql = "SELECT c FROM $this->entity c WHERE c.country.id=?1";
    
    	return $this->getEntityManager()->createQuery($dql)
    	->setParameter(1, $countryId)
    	->setMaxResults($number)
    	->getResult();
    }
    
    
    public function getByUserFacebookUID($userFacebookUID,$number = 30)
    {
    	$dql = "SELECT c FROM $this->entity c WHERE IDENTITY(c.userFacebook)=?1";
    
    	return $this->getEntityManager()->createQuery($dql)
    	->setParameter(1, $userFacebookUID)
    	->setMaxResults($number)
    	->getResult();
    }
    public function getByUserFacebookUIDEmail($userFacebookUID,$email,$number = 1)
    {
    	$dql = "SELECT c FROM $this->entity c WHERE IDENTITY(c.userFacebook)=?1 OR c.email=?2";
    
    	return $this->getEntityManager()->createQuery($dql)
    	->setParameter(1, $userFacebookUID)
    	->setParameter(2, $email)
    	->setMaxResults($number)
    	->getResult();
    }
    
    public function getListUser($arrayUserId)
    {
    	$dql = "SELECT c FROM $this->entity c WHERE";
    	$count=0;
    	foreach ($arrayUserId as $userId)
    	{
    		if($count!=0)
    		{
    			$dql.=" OR";
    		}
    		$count++;
    		$dql.=" c.uid=?$count";
    	}
    	$result=$this->getEntityManager()->createQuery($dql);
    	$count=0;
    	foreach ($arrayUserId as $userId)
    	{
    		$count++;
    		$result->setParameter($count, $userId);
    	}
    	return $result->getResult();
    }
    
}